using System.Reflection;
using System.Text;
using Example.Command.Kernel;
using SuperSocket.Command;
using SuperSocket.ProtoBase;
using SuperSocket.Server.Abstractions.Session;

namespace Example.Command.Handler;

/// <summary>
/// 求和运算
///
/// Command特性:
/// - Key  参数用于指定命令的唯一标识符
/// - Name 参数用于指定命令的名称。这个名称可以用于日志记录、调试或其他需要识别命令的场景。
/// </summary>
[TraceFilter]
[Command(Key = "1001", Name = "add")]
public class Add : ICommand<StringPackageInfo> 
{
    public void Execute (IAppSession session, StringPackageInfo package) {
        var cmd = $"{this.GetType().GetCustomAttribute<CommandAttribute>()?.Name}";
        
        Thread.Sleep(3000);//模拟耗时
        
        // var result = package.Parameters.Select(int.Parse).Aggregate((a, b) => (a + b));
        var result = package.Parameters.Select(int.Parse).Sum();
        session.SendAsync(Encoding.UTF8.GetBytes($"=> [{cmd}]命令被执行,结果是: {result} \r\n\r\n"));
    }
}